package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.context.BaseContext;
import com.honglv.mapper.CommodityMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadCommodityDTO;
import com.honglv.pojo.entity.Commodities;
import com.honglv.pojo.entity.Commodities;
import com.honglv.pojo.entity.Commodities;
import com.honglv.pojo.vo.GetCommoditiesByCityVO;
import com.honglv.pojo.vo.GetCommoditiesByCityVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CommodityServiceImpl implements CommodityService {
    
    @Autowired
     private CommodityMapper commodityMapper;
    /**
     * 管理员上传文创
     * @param
     * @return
     */
    @Override
    public void upload(UploadCommodityDTO uploadCommodityDTO) {
        Long authorId = BaseContext.getCurrentId();
        Commodities commodities=new Commodities();
        BeanUtils.copyProperties(uploadCommodityDTO,commodities);
        commodities.setAuthorId(authorId);
        commodityMapper.insert(commodities);


    }
    /**
     * 根据城市分页获取文创
     * @param
     * @return
     */
    @Override
    public PageVO<GetCommoditiesByCityVO> getCommoditiesByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Commodities> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Commodities> list = commodityMapper.selectList(qw);
        GetCommoditiesByCityVO gf=new GetCommoditiesByCityVO();
        List<GetCommoditiesByCityVO> commodities=new ArrayList<>();
        for(Commodities Commodities:list){
            BeanUtils.copyProperties(Commodities,gf);
            commodities.add(gf);
        }
        if (commodities == null){
            return null;
        }
        PageInfo<GetCommoditiesByCityVO> pageInfo = new PageInfo<>(commodities);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }

}
        
    

